package org.jeecg.modules.quartz.job;

import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.dto.message.TemplateMessageDTO;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.demo.puInvoice.entity.PuInvoice;
import org.jeecg.modules.demo.puInvoice.service.IPuInvoiceService;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysAnnouncementService;
import org.jeecg.modules.system.service.ISysUserDepartService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author HuFuGui
 * @version 1.0
 * @description 1.0
 * @date 2022-11-13 15:50
 */
@Slf4j
public class PuPaymentJob implements Job {

    @Autowired
    private IPuInvoiceService puInvoiceService;
    @Autowired
    private ISysUserDepartService sysUserDepartService;
    @Autowired
    private ISysBaseAPI sysBaseAPI;

    private static final String TITLE = "付款截止时间预警";
    private static final String SENDER = "管理员";


    private static final String GL="e9ca23d68d884d4ebb19d07889727dae";

    private static final String XMGL = "1472934861319970817";

    private  String userid= ","+GL+","+XMGL+",";
//    private  String  departUserId;

    private String getMessage(String name,String date,String day) {
        String str ="由【"+name+"】于【"+date+"】发起的采购发票还有"+day+"天就到截至付款日期了，请查看单据是否付款！";
        return str;
    }

    private String parameter;

    public void setParameter(String parameter) {
        this.parameter = parameter;
    }
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        List<PuInvoice> puInvoices = puInvoiceService.getPaymentJob(parameter);
        //查询部门员工
//        String departId = puInvoiceService.getDepartId();
//        List<SysUser> sysUsers = sysUserDepartService.queryUserByDepId(departId);
//
//        for (SysUser sysUser : sysUsers) {
//            if(sysUser.getId().equals(GL)){
//                continue;
//            }else{
//             departUserId = sysUser.getId()+",";
//            }
//        }

        if (!puInvoices.isEmpty()) {
            for (PuInvoice puInvoice : puInvoices) {
                LoginUser user = sysBaseAPI.getUserById(puInvoice.getPoStaff());
                TemplateMessageDTO templateMessageDTO = new TemplateMessageDTO();
                templateMessageDTO.setFromUser(SENDER);
                if(puInvoice.getPoStaff().equals(XMGL)){
                    templateMessageDTO.setToUser(puInvoice.getPoStaff()+"," + GL+","+puInvoice.getSaleman());
                }else {
                    templateMessageDTO.setToUser(puInvoice.getPoStaff()+ userid+puInvoice.getSaleman());
                }
                templateMessageDTO.setTitle(TITLE);
                templateMessageDTO.setTemplateCode("order");
                Map<String, String> templateParam = new HashMap<>();
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String str= simpleDateFormat.format(puInvoice.getPuiCdate());
                templateParam.put("ts_date",str);
                templateParam.put("ts_content",getMessage(user.getRealname(),str,parameter));
                templateParam.put("checkOrder",puInvoice.getPuiNo());
                templateParam.put("cd_url","/puinvoice/puInvoice/list");
                templateMessageDTO.setTemplateParam(templateParam);
                templateMessageDTO.setTenantId(puInvoice.getTenantId());
                sysBaseAPI.sendTemplateAnnouncement1(templateMessageDTO);

            }
            log.info(String.format(" 采购付款时间定时任务（有数据） !  数据条数:"+puInvoices.size() +"!   时间："+ DateUtils.getTimestamp()));
        }
        log.info(String.format(" 采购付款时间定时任务（无数据） ! 时间："+ DateUtils.getTimestamp()));
    }
}
